package com.to.aboomy.bannersample.slice;

import com.to.aboomy.banner.Banner;
import com.to.aboomy.banner.BannerViewPager;
import com.to.aboomy.banner.IndicatorView;
import com.to.aboomy.banner.ScalePageTransformer;
import com.to.aboomy.bannersample.ResourceTable;
import com.to.aboomy.bannersample.pagetransformer.*;
import com.to.aboomy.bannersample.util.ArrayStringItemSelectDialog;
import com.to.aboomy.bannersample.util.ToastUtil;
import com.to.aboomy.bannersample.util.Utils;
import com.to.aboomy.bannersample.viewpager.creator.ImageHolderCreator;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.utils.Color;

import java.util.Arrays;
import java.util.List;

/**
 * 切换动画示例
 *
 * @since 2021-08-02
 */
public class AnimAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    public static final String[] ANIMS = {
            "DefaultTransformer",
            "AccordionTransformer",
            "BackgroundToForegroundTransformer",
            "ForegroundToBackgroundTransformer",
            "CubeInTransformer",
            "CubeOutTransformer",
            "DepthPageTransformer",
            "FlipHorizontalTransformer",
            "FlipVerticalTransformer",
            "RotateDownTransformer",
            "RotateUpTransformer",
            "ScaleInOutTransformer",
            "StackTransformer",
            "TabletTransformer",
            "ZoomInTransformer",
            "ZoomOutTransformer",
            "ZoomOutSlideTransformer",
    };

    public static final BannerViewPager.PageTransformer[] TRANSFORMERS = {
            null,
            new AccordionTransformer(),
            new BackgroundToForegroundTransformer(),
            new ForegroundToBackgroundTransformer(),
            null,//CubeInTransformer
            null,//CubeOutTransformer
            new DepthPageTransformer(),
            null,//FlipHorizontalTransformer
            null,//FlipVerticalTransformer
            new RotateDownTransformer(),
            new RotateUpTransformer(),
            new ScaleInOutTransformer(),
            null,//StackTransformer
            null,//TabletTransformer
            null,//new ZoomInTransformer()
            null,//new ZoomOutTransformer()
            new ZoomOutSlideTransformer(),

    };

    private static final String[] ANIMS2 = {
            "NonPageTransformer",
            "AlphaPageTransformer",
            "RotateDownPageTransformer",
            "RotateUpPageTransformer",
            "RotateYTransformer",
            "ScaleInTransformer",
    };

    public static final BannerViewPager.PageTransformer[] TRANSFORMERS2 = {
            null,
            new AlphaPageTransformer(),
            new RotateDownPageTransformer(),
            new RotateUpPageTransformer(),
    };
    private int initChoose1,initChoose2;
    Banner banner,banner2;
    Button selectAnim,selectAnim2;
    List<Integer> imageBanner1;
    List<Integer> imageBanner2;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_anim);

        banner = (Banner) findComponentById(ResourceTable.Id_banner);
        banner2 = (Banner) findComponentById(ResourceTable.Id_banner2);
        selectAnim = (Button) findComponentById(ResourceTable.Id_selectAnim);
        selectAnim2 = (Button) findComponentById(ResourceTable.Id_selectAnim2);
        selectAnim.setClickedListener(this);
        selectAnim2.setClickedListener(this);
        initBanner();
        initBanner2();
    }

    /**
     * initBanner
     */
    private void initBanner() {
        imageBanner1 = Utils.getImage(5);

        banner.setHolderCreator(new ImageHolderCreator())
                .setIndicator(new IndicatorView(this)
                        .setIndicatorColor(Color.GRAY)
                        .setIndicatorSelectorColor(Color.WHITE))
                .setPages(imageBanner1);
    }

    /**
     * initBanner2
     */
    private void initBanner2() {
        imageBanner2 = Utils.getImage(5);
        banner2.setHolderCreator(new ImageHolderCreator())
                .setIndicator(new IndicatorView(this)
                        .setIndicatorColor(Color.GRAY)
                        .setIndicatorSelectorColor(Color.WHITE))
                .setPageMargin(-(AttrHelper.vp2px(40,getContext())),AttrHelper.vp2px(10,getContext()))
                .setPageTransformer(true, new AlphaPageTransformer())
                .setPages(imageBanner2);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()){
            case ResourceTable.Id_selectAnim:
                new ArrayStringItemSelectDialog(getAbility(), Arrays.asList(ANIMS))
                        .setChoose(initChoose1)
                        .setOnItemClickListener((position, value) -> {
                            initChoose1 = position;
                            selectAnim.setText(value);
                            if(position != 0){
                                if(TRANSFORMERS[position] != null){
                                    banner.setPageTransformer(true,TRANSFORMERS[position]);
                                    banner.setPages(imageBanner1);
                                }else{
                                    ToastUtil.showToast(getAbility(),value+"动画暂不支持");
                                }
                            }
                        }).show();
                break;
            case ResourceTable.Id_selectAnim2:
                new ArrayStringItemSelectDialog(getAbility(),Arrays.asList(ANIMS2))
                        .setChoose(initChoose2)
                        .setOnItemClickListener((position, value) -> {
                            initChoose2 = position;
                            selectAnim2.setText(value);
                            if(TRANSFORMERS2[position] != null){
                                banner2.setPageTransformer(true, TRANSFORMERS2[position]);
                            }
                            banner2.setPages(imageBanner2);
                        }).show();
                break;

            default:
                break;
        }
    }
}
